/**
 * 系统公用JS函数库
 * content	string				内容
 * type		alert-success		成功
 * 			alert-info			信息
 * 			alert-error			错误
 */
var top_px  = 0;
function tke_alert(content,type,time) {
	
	top_px = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
	
	if($("#msg_box").size() > 0){
		$("#msg_box").html("\<button class='close' data-dismiss='alert' type='button'\>×\<\/button\>\<strong\>提示：\<\/strong\>	" + content).attr("class",'alert ' + type ).hide().fadeIn(300);
	}else{
		$("#breadcrumb").after("\<div id='msg_box' class='alert "+type+"' \>	\<button class='close' data-dismiss='alert' type='button'\>×\<\/button\>\<strong\>提示：\<\/strong\>	" + content + "\<\/div\>")
	}
	
	if(top_px > 77){
		$("#msg_box").addClass('scroll_alert')
		$("#msg_box .close").addClass('scroll_alert_close')
	}
	
	if(time > 0){
		setTimeout(function(){
			$("#msg_box").fadeOut(500);
		},time);
	}
}
/**
 * @todo 默认将焦点移动到form表单第一个input上
 * @author PHP@妖孽 <dino_ma@163.com>
 */
function tke_form_focus() {
	$('form input:first').focus();
}
/**
 * @todo 提示自动置顶
 */
$(window).scroll(function(){
	
	top_px = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
	
	if(top_px <= 77){
		$('#msg_box').removeClass('scroll_alert');
		$("#msg_box .close").removeClass('scroll_alert_close')
	}else{
		$("#msg_box").addClass('scroll_alert')
		$("#msg_box .close").addClass('scroll_alert_close')
	}
});

/**
 * @todo ajax-get提交
 * @author PHP@妖孽 <dino_ma@163.com>
 */
$('.ajax-get').click(function(){
    var target;
    var that = this;
    if ( $(this).hasClass('confirm') ) {
        if(!confirm('确认要执行该操作吗?')){
            return false;
        }
    }
    if ( (target = $(this).attr('href')) || (target = $(this).attr('url')) ) {
        $.get(target).success(function(data){
            if (data.status==1) {
                if (data.url) {
                	tke_alert(data.info + ' 将自动跳转!','alert-success');
                }else{
                	tke_alert(data.info,'alert-success');
                }
                setTimeout(function(){
                	 if (data.url) {
                         location.href=data.url;
                     }else{
                         location.reload();
                     }
                },1500);
            }else{
            	tke_alert(data.info,'alert-error');
                setTimeout(function(){
                	 if (data.url) {
                         location.href=data.url;
                     }else{
                         location.reload();
                     }
                },1500);
            }
        });

    }
    return false;
});

/**
 * @todo ajax-post提交
 * @author PHP@妖孽 <dino_ma@163.com>
 */
$('.ajax-post').click(function(){
    var query,form;
    var that = this;
    if ( $(this).hasClass('confirm') ) {
        if(!confirm('确认要执行该操作吗?')){
            return false;
        }
    }
    var target_form = $(that).attr('target-form');
    var url = $(that).attr('url');
    
    if((!target_form) || (!target_form) || (!url)){
    	return false;
    }
    var target = $('.'+target_form);
   	var query = target.serialize();
    $.post(url,query).success(function(data){
        if (data.status==1) {
            if (data.url) {
                tke_alert(data.info + ' 将自动跳转!','alert-success');
            }else{
                tke_alert(data.info ,'alert-success');
            }
            setTimeout(function(){
                if (data.url) {
                    location.href=data.url;
                }else{
                    location.reload();
                }
            },1500);
        }else{
            tke_alert(data.info,'alert-error');
            setTimeout(function(){
                if (data.url) {
                    location.href=data.url;
                }
            },1500);
        }
    });
    return false;
});


/**
 * @todo  默认值加载
 * @author 能万
 */
function set_value(name,value){
	var first = name.substr(0,1), input, i = 0, val;
	if(value === "") return;
	if("#" === first || "." === first){
		input = $(name);
	} else {
		input = $("[name='" + name + "']");
	}

	if(input.eq(0).is(":radio")) { 
		input.filter("[value='" + value + "']").each(function(){this.checked = true});
		input.filter("[value='" + value + "']").parents("span").attr("class","checked");
	} else if(input.eq(0).is(":checkbox")) { 
		if(!$.isArray(value)){
			val = new Array();
			val[0] = value;
		} else {
			val = value;
		}
		for(i = 0, len = val.length; i < len; i++){
			input.filter("[value='" + val[i] + "']").each(function(){this.checked = true});
		}
	} else {
		input.val(value);
		input.parents('.control-group').find('span').html(input.find("option:selected").text())
	}
}


/**
 * @todo 组装Url
 * @author PHP@妖孽 <dino_ma@163.com>
 */
function checkUrl(url,query){
    query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
    query = query.replace(/^&/g,'');
    if( url.indexOf('?')>0 ){
        url += '&' + query;
    }else{
        url += '?' + query;
    }
    return url;
}

/**
 * @todo 增加日历插件
 * @author PHP@妖孽 <dino_ma@163.com>
 */
if('undefined' !== typeof datetimepicker){
	$('.datepicker').datetimepicker({
	    language: 'en',
	    pickDate: true,
	    pickTime: true,
	    hourStep: 1,
	    minuteStep: 15,
	    secondStep: 30,
	    inputMask: true
	  });
}

//搜索功能
$("#search_button").click(function(){
	var url = $(this).attr('url');
    var query  = $('.search-form').find('input,select').serialize();
    //query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
    query = query.replace(/^&/g,'');
    if( url.indexOf('?')>0 ){
        url += '&' + query;
    }else{
        url += '?' + query;
    }
	window.location.href = url;
});
//回车搜索
$(".search-input").keyup(function(e){
	if(e.keyCode === 13){
		$("#search_button").click();
		return false;
	}
});
//用于页面非ajax跳转
$('.window-jump').click(function(){
	window.location.href=$(this).attr('url');
});

/**
 * @todo 自动移动下一个focus
 */
$.fn.autoNext = function(){
	if($(this).length){
		// 第一个框聚焦
		var $input_group = $(this);
		$input_group.eq(0).focus();
		// 指定input的class为n-auto-next
		$input_group.each(function(i) {
			$(this).attr("order", i);
		});
		// 回车自动触发切换到下一个input
		$input_group.keydown(function(event) {
			if ( 13 == event.keyCode){
				var order = $(this).attr("order");
				order++;
				var $input = $input_group.eq(order).focus();
			}
		});		
	}
}

/**
 * @todo 活动商户联动
 * @param activity_id
 * @param url
 * @author sap
 */
function change_merchant(activity_id,url){
	var activity_id = parseInt($(activity_id).val());
	if(activity_id !== 0){
		$.post(url,{activity_id : activity_id,data_type:1},function(backData){
			//商户选项
			$('#merchant_id').empty();
			var option = '';
			option += '<option value="0">选择商户</option>';
			if(backData.status == 1){
				$.each(backData.data,function(index,array){
					option += '<option value="'+array['merchant_id']+'">'+array['merchant_name']+'</option>';
				})
			}
			$('#merchant_id').append(option);
			set_value("merchant_id", 0);
			//店铺置空
			$('#shop_id').empty();
			$('#shop_id').append('<option value="0">选择店铺</option>');
			set_value("shop_id", 0);
		})
	}else{
		$('#merchant_id').empty();
		$('#merchant_id').append('<option value="0">选择商户</option>');
		set_value("merchant_id", 0);
		$('#shop_id').empty();
		$('#shop_id').append('<option value="0">选择店铺</option>');
		set_value("shop_id", 0);
	}
}

/**
 * @todo 商户店铺联动
 * @param int activity_id
 * @param int merchant_id
 * @param string url
 * @author sap
 */
function change_shop(activity_id,merchant_id,url){
	var activity_id = parseInt($(activity_id).val());
	var merchant_id = parseInt($(merchant_id).val());
	if(activity_id !== 0 && merchant_id !== 0){
		$.post(url,{activity_id:activity_id,merchant_id : merchant_id,data_type:1},function(backData){
			$('#shop_id').empty();
			var option = '';
			option += '<option value="0">选择店铺</option>';
			if(backData.status == 1){
				$.each(backData.data,function(index,array){
					option += '<option value="'+array['shop_id']+'">'+array['shop_name']+'</option>';
				})
			}
			$('#shop_id').append(option);
			set_value("shop_id", 0);
		})
	}else{
		$('#shop_id').empty();
		$('#shop_id').append('<option value="0">选择店铺</option>');
		set_value("shop_id", 0);
	}
}



/**
 * @todo 商户与店铺联动
 * @param int merchant_id
 * @param string url
 * @author fzh
 */
function change_shop_list(merchant_id,url){
	var merchant_id = parseInt(merchant_id);
	if(merchant_id !== 0){
		$.post(url,{merchant_id : merchant_id},function(backData){
			$('#shop_id').empty();
			var option = '';
			option += '<option value="0">选择店铺</option>';
			$.each(backData,function(index,array){
				option += '<option value="'+array['shop_id']+'">'+array['shop_name']+'</option>';
			})
			$('#shop_id').append(option);
			set_value("shop_id", 0);
			
			$('#goods_category_pid').empty();
			$('#goods_category_pid').append('<option value="0">选择一级品类</option>');
			set_value("goods_category_pid", 0);
			
			$('#goods_category_id').empty();
			$('#goods_category_id').append('<option value="0">选择二级品类</option>');
			set_value("goods_category_id", 0);
		})
	}else{
		$('#shop_id').empty();
		$('#shop_id').append('<option value="0">选择店铺</option>');
		set_value("shop_id", 0);
		
		$('#goods_category_pid').empty();
		$('#goods_category_pid').append('<option value="0">选择一级品类</option>');
		set_value("goods_category_pid", 0);
		
		$('#goods_category_id').empty();
		$('#goods_category_id').append('<option value="0">选择二级品类</option>');
		set_value("goods_category_id", 0);
	}
}

/**
 * @todo 店铺与店铺经营品类的联动
 * @param int shop_id
 * @param string url
 * @author fzh
 */
function change_goods_category_pid(shop_id,url){
	var shop_id = parseInt(shop_id);
	if(shop_id !== 0){
		$.post(url,{shop_id : shop_id},function(backData){
			$('#goods_category_pid').empty();
			var option = '';
			option += '<option value="0">选择一级品类</option>';
			$.each(backData,function(index,array){
				option += '<option value="'+array['goods_category_id']+'">'+array['goods_category_name']+'</option>';
			})
			$('#goods_category_pid').append(option);
			set_value("goods_category_pid", 0);
			
			$('#goods_category_id').empty();
			$('#goods_category_id').append('<option value="0">选择二级品类</option>');
			set_value("goods_category_id", 0);
		})
	}else{
		$('#goods_category_pid').empty();
		$('#goods_category_pid').append('<option value="0">选择一级品类</option>');
		set_value("goods_category_pid", 0);
		
		$('#goods_category_id').empty();
		$('#goods_category_id').append('<option value="0">选择二级品类</option>');
		set_value("goods_category_id", 0);
	}
}


/**
 * @todo 店铺与店铺经营品类的联动
 * @param int shop_id
 * @param string url
 * @author fzh
 */
function change_goods_category_id(goods_category_id,url){
	var goods_category_id = parseInt(goods_category_id);
	if(goods_category_id !== 0){
		$.post(url,{goods_category_id : goods_category_id},function(backData){
			$('#goods_category_id').empty();
			var option = '';
			option += '<option value="0">选择二级品类</option>';
			$.each(backData,function(index,array){
				option += '<option value="'+array['goods_category_id']+'">'+array['goods_category_name']+'</option>';
			})
			$('#goods_category_id').append(option);
			set_value("goods_category_id", 0);
		})
	}else{
		$('#goods_category_id').empty();
		$('#goods_category_id').append('<option value="0">选择二级品类</option>');
		set_value("goods_category_id", 0);
	}
}

/**
 * @todo 添加文章标签分类二级联动
 * @param int ask_category_pid
 * @param string url
 * @param int 
 * @author Cruise
 */
function change_ask_category(ask_category_id,url,num){
	$('#ask_category_id').empty();
	$('#ask_category_id').select2();
	var ask_category_id = parseInt(ask_category_id);
	var num = parseInt(num);
	if(ask_category_id !== 0){
		$.post(url,{ask_category_id : ask_category_id},function(backData){
			$('#ask_category_id').empty();
			var option = '';
			if(num === 1){
				option += '<option value="0">选择二级分类</option>';
			}
			if(backData === null){
				if(num === 1){
					set_value("ask_category_id", 0);
				}
			}else{
				$.each(backData,function(index,array){
					option += '<option value="'+array['ask_category_id']+'">'+array['ask_category_name']+'</option>';
				})
			}
			$('#ask_category_id').append(option);
			if(num === 1){
				set_value("ask_category_id", 0);
			}
		})
	}else{
		$('#ask_category_id').empty();
		$('#ask_category_id').append('<option value="0" >选择二级分类</option>');
	}
}

